package com.yungu.swift.admin.route.marketing;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminStatusParam;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.system.marketing.model.dto.MarketingActivityDto;
import com.yungu.swift.system.marketing.model.param.AdminActivityParam;
import com.yungu.swift.system.marketing.model.param.AdminActivitySaveParam;
import com.yungu.swift.system.marketing.model.param.AdminActivitySortParam;
import com.yungu.swift.system.marketing.model.vo.AdminActivityVo;
import com.yungu.swift.system.marketing.service.MarketingActivityService;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/8/1
 */
@RestController
@RequestMapping("/admin/v1/marketing/activity")
@Api(value = "【营销管理】", tags = "APP活动管理")
public class ActivityAdmin {

    @Reference
    private MarketingActivityService marketingActivityService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "活动列表", httpMethod = "POST")
    public ResponseData<AdminActivityVo> list(@RequestBody AdminActivityParam activityParam) {
        if (ParamUtil.isIllegal(activityParam.getBusinessType(), activityParam.getShowType())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return marketingActivityService.list(activityParam);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "活动新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminActivitySaveParam activitySaveParam) {
        if (ParamUtil.isIllegal(activitySaveParam.getTitle(), activitySaveParam.getCityUuid(),
                activitySaveParam.getPutInDateOn(), activitySaveParam.getPutInDateOff(), activitySaveParam.getStatus(),
                activitySaveParam.getBusinessType(), activitySaveParam.getShowType())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return marketingActivityService.saveOrUpdate(activitySaveParam);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation(value = "删除活动", httpMethod = "POST")
    public ResponseData<Boolean> delete(@RequestBody AdminUuidParam uuidParam) {
        if (ParamUtil.isIllegal(uuidParam.getUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return marketingActivityService.del(uuidParam.getUuid(), MarketingActivityDto.class);
    }

    @RequestMapping(value = "/setStatus", method = RequestMethod.POST)
    @ApiOperation(value = "开启或关闭活动", httpMethod = "POST")
    public ResponseData<Boolean> setStatus(@RequestBody AdminStatusParam statusParam) {
        if (ParamUtil.isIllegal(statusParam.getUuid(), statusParam.getStatus())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return marketingActivityService.setActive(statusParam);
    }

    @RequestMapping(value = "/updateSort", method = RequestMethod.POST)
    @ApiOperation(value = "更新排序", httpMethod = "POST")
    public ResponseData<Boolean> updateSort(@RequestBody List<AdminActivitySortParam> list) {
        return marketingActivityService.updateSort(list);
    }
}
